package com.example.demo.executor.backupDeal;

import com.example.demo.DBServer.targetDatabase.DBServerDao;
import com.example.demo.executor.sqlGenerate.SqlGenerator;
import com.example.demo.executor.sqlGenerate.SqlGeneratorFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 默认备份表处理器
 * 直接删除备份表
 */
@Component
public class DefaultBackupDataExecutor implements BackupDataExecutor{

    @Autowired
    private DBServerDao dbServerDao;

    @Autowired
    private SqlGeneratorFactory sqlGeneratorFactory;

    @Value("${database.software.name}")
    private String databaseSoftwareName;

    private SqlGenerator sqlGenerator;

    @PostConstruct
    public void init(){
        SqlGenerator sqlGenerator = sqlGeneratorFactory.getSqlGenerator(databaseSoftwareName);
        if(sqlGenerator == null){
            throw new NullPointerException();
        }
        this.sqlGenerator = sqlGenerator;
    }

    /**
     * 直接删除备份表
     * @param backupTableName 备份表名称
     */
    @Override
    public void backupTableDataDeal(String backupTableName) {
        String dropSql = sqlGenerator.generateDropTableSql(backupTableName);
        dbServerDao.dropTable(dropSql);
    }

    @Override
    public String getDealStrategy() {
        return BackupDealEnum.DEFAULT.getDealStrategy();
    }
}
